package b.h.y.bj.base.impl;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;

import b.h.y.bj.MainActivity;
import b.h.y.bj.base.BaseMenuDetailPager;
import b.h.y.bj.base.BasePager;
import b.h.y.bj.base.menudetail.InteractMenuDetailPager;
import b.h.y.bj.base.menudetail.NewsMenuDetailPager;
import b.h.y.bj.base.menudetail.PhotoMenuDetailPager;
import b.h.y.bj.base.menudetail.TopicMenuDetailPager;
import b.h.y.bj.domain.NewsData;
import b.h.y.bj.fragment.LeftMenuFragment;
import b.h.y.bj.global.GlobalContants;
import b.h.y.bj.util.CacheUtils;

/**
 * 新闻中心实现
 * Created by YHB on 2015/12/31.
 */
public class NewsCenterPager extends BasePager {

    private ArrayList<BaseMenuDetailPager> mPagers;
    private NewsData newsData;

    public NewsCenterPager(Activity activity) {
        super(activity);
    }

    @Override
    public void initData() {
        setSlidingMenuEnable(true);

        String cache = CacheUtils.getCache(mActivity, GlobalContants.CATEGORIES_URL);

        if (!TextUtils.isEmpty(cache)) {
            parseData(cache);
        }
        //不管有没有缓存都获取下最新的数据
        fetchDataFromServer();
    }

    private void fetchDataFromServer() {
        HttpUtils utils = new HttpUtils();

        //发送请求
        utils.send(HttpRequest.HttpMethod.GET, GlobalContants.CATEGORIES_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                CacheUtils.setCache(mActivity, GlobalContants.CATEGORIES_URL, result);
                parseData(result);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });
    }

    /**
     * 解析成JSON
     *
     * @param result
     */
    private void parseData(String result) {
        Gson gson = new Gson();

        newsData = gson.fromJson(result, NewsData.class);

        //刷新侧边栏的数据
        MainActivity mainActivity = (MainActivity) mActivity;
        LeftMenuFragment leftMenuFragment = mainActivity.getLeftMenuFragment();
        leftMenuFragment.setMenuData(newsData);

        //准备4个菜单详情页
        mPagers = new ArrayList<BaseMenuDetailPager>();
        mPagers.add(new NewsMenuDetailPager(mainActivity, newsData.data.get(0).children));
        mPagers.add(new TopicMenuDetailPager(mainActivity));
        mPagers.add(new PhotoMenuDetailPager(mainActivity, mBtnPhotoType));
        mPagers.add(new InteractMenuDetailPager(mainActivity));

        setCurrentMenuDetailPager(0); //设置菜单详情页-新闻为默认的首页
    }

    /**
     * 设置当前菜单详情页
     *
     * @param position
     */
    public void setCurrentMenuDetailPager(int position) {
        BaseMenuDetailPager baseMenuDetailPager = mPagers.get(position); //获取当前要显示的菜单详情页
        flContent.removeAllViews();
        flContent.addView(baseMenuDetailPager.mRootView); //将布局文件设置给frameLayout

        //设置当前页的标题
        NewsData.NewsMenuData newsMenuData = newsData.data.get(position);
        tvTitle.setText(newsMenuData.title);

        //初始化当前页面的数据
        baseMenuDetailPager.initData();

        if (baseMenuDetailPager instanceof PhotoMenuDetailPager) {
            mBtnPhotoType.setVisibility(View.VISIBLE);
        } else {
            mBtnPhotoType.setVisibility(View.INVISIBLE);
        }
    }
}
